<script setup lang="ts">
import { uploadImageAPI } from '@/apis/consult'
import { useConsultStore } from '@/stores/consult'
import type { PostPatientConsultOrderParams } from '@/types/consult'
import { ref } from 'vue'

const illnessInfo = ref<
  Pick<PostPatientConsultOrderParams, 'illnessDesc' | 'illnessTime' | 'consultFlag' | 'pictures'>
>({
  illnessDesc: '',
  illnessTime: 1,
  consultFlag: 0,
  pictures: [],
})
const illnessTimes = ref([
  { label: '一周内', value: 1 },
  { label: '一个月内', value: 2 },
  { label: '半年内', value: 3 },
  { label: '大于半年', value: 4 },
])
const consultFlags = ref([
  { label: '就诊过', value: 1 },
  { label: '没有就诊过', value: 0 },
])

// 选择文件后触发，用于自定义上传图片
const handleSelect = async (event: UniHelper.UniFilePickerOnSelectEvent) => {
  // 获取文件路径
  const filePath = event.tempFilePaths[0]
  const tempFile = event.tempFiles[0]
  // 上传文件
  const { data } = await uploadImageAPI(filePath)
  // 添加到图片列表
  illnessInfo.value.pictures?.push({
    id: data.id,
    url: data.url,
    name: tempFile.name, // 兼容 uni-file-picker 的回显
    extname: tempFile.extname, // 兼容 uni-file-picker 的回显})
  })
  console.log('illnessInfo.value.pictures', illnessInfo.value.pictures)
}
const handleDelete = (event: UniHelper.UniFilePickerOnDeleteEvent) => {
  console.log(event)
  illnessInfo.value.pictures = illnessInfo.value.pictures?.filter(
    (item, index) => index !== event.index
  )
}
const store = useConsultStore()
if (store.illnessInfo1.illnessDesc) {
  uni.showModal({
    title: '温馨提示',
    content: '是否恢复之前填写的病情信息？',
    showCancel: true,
    confirmColor: '#16C2A3',
    cancelColor: '#848484',
    success: ({ confirm, cancel }) => {
      if (confirm) {
        illnessInfo.value = store.illnessInfo1
      }
    },
  })
}
const props = defineProps<{
  type: string
  depId: string
  illnessType: string
}>()
const handleNextType = () => {
  store.illnessInfo1 = illnessInfo.value
  store.illnessType = props.illnessType
  store.depId = props.depId
  store.type = props.type
  uni.navigateTo({ url: '/subpkg_archive/pages/list/list?type=consult' })
}
</script>

<template>
  {{ illnessInfo }}
  <scroll-page>
    <view class="description-page">
      <!-- 在线医生 -->
      <view class="doctor-online">
        <image src="/static/uploads/doctor-avatar-2.png" class="doctor-avatar" />
        <view class="doctor-info">
          <view class="name">在线医生</view>
          <view class="message">
            请描述你的疾病或症状、是否用药、就诊经历，需要我听过什么样的帮助
          </view>
          <view class="tips">
            <text class="iconfont icon-shield"></text>
            内容仅医生可见
          </view>
        </view>
      </view>
      <!-- 患者信息 -->
      <view class="patient-info">
        <view class="description">
          <uni-easyinput
            v-model="illnessInfo.illnessDesc"
            type="textarea"
            :input-border="false"
            placeholder-style="font-size: 30rpx; color: #979797"
            placeholder="请详细描述您的病情,病情描述不能为空"
          />
        </view>

        <view class="title">本次患病多久了？</view>
        <view class="tags">
          <text
            class="tag"
            v-for="item in illnessTimes"
            :key="item.value"
            :class="{ active: illnessInfo.illnessTime === item.value }"
            @click="illnessInfo.illnessTime = item.value"
            >{{ item.label }}</text
          >
        </view>

        <view class="title">此次病情是否去医院就诊过？</view>
        <view class="tags">
          <text
            class="tag"
            v-for="item in consultFlags"
            :key="item.value"
            :class="{ active: illnessInfo.consultFlag === item.value }"
            @click="illnessInfo.consultFlag = item.value"
            >{{ item.label }}</text
          >
        </view>
      </view>
      <!-- 上传图片 -->
      <view class="patient-picture">
        <uni-file-picker
          title="上传病情相关图片 (仅医生可见)"
          limit="8"
          :image-styles="{ width: '160rpx', height: '160rpx' }"
          file-extname="png,jpg,gif,webp"
          :auto-upload="false"
          :value="illnessInfo.pictures"
          @select="handleSelect"
          @delete="handleDelete"
        />
      </view>
    </view>
    <!-- 下一步操作 -->
    <view class="next-step">
      <button class="uni-button" :disabled="illnessInfo.illnessDesc === ''" @click="handleNextType">
        下一步
      </button>
    </view>
  </scroll-page>
  {{ store }}
</template>

<style lang="scss">
@import './index.scss';
</style>
